// /import core
// /import uicore
(function() {
	var utils = baidu.editor.utils, uiUtils = baidu.editor.ui.uiUtils, domUtils = baidu.editor.dom.domUtils, UIBase = baidu.editor.ui.UIBase, Popup = baidu.editor.ui.Popup = function(
			options) {
		this.initOptions(options);
		this.initPopup();
	};

	var allPopups = [];
	function closeAllPopup(el) {
		var newAll = [];
		for (var i = 0; i < allPopups.length; i++) {
			var pop = allPopups[i];
			if (!pop.isHidden()) {
				if (pop.queryAutoHide(el) !== false) {
					pop.hide();
				}
			}
		}
	}

	Popup.postHide = closeAllPopup;

	var ANCHOR_CLASSES = ['edui-anchor-topleft', 'edui-anchor-topright',
			'edui-anchor-bottomleft', 'edui-anchor-bottomright'];
	Popup.prototype = {
		SHADOW_RADIUS : 5,
		content : null,
		_hidden : false,
		autoRender : true,
		canSideLeft : true,
		canSideUp : true,
		initPopup : function() {
			this.initUIBase();
			allPopups.push(this);
		},
		getHtmlTpl : function() {
			return '<div id="##" class="edui-popup %%">'
					+ ' <div id="##_body" class="edui-popup-body">'
					+ ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: white;" frameborder="0" width="100%" height="100%" src="javascript:"></iframe>'
					+ ' <div class="edui-shadow"></div>'
					+ ' <div id="##_content" class="edui-popup-content">'
					+ this.getContentHtmlTpl() + '  </div>' + ' </div>'
					+ '</div>';
		},
		getContentHtmlTpl : function() {
			if (typeof this.content == 'string') {
				return this.content;
			}
			return this.content.renderHtml();
		},
		_UIBase_postRender : UIBase.prototype.postRender,
		postRender : function() {
			if (this.content instanceof UIBase) {
				this.content.postRender();
			}
			this.hide(true);
			this._UIBase_postRender();
		},
		_doAutoRender : function() {
			if (!this.getDom() && this.autoRender) {
				this.render();
			}
		},
		mesureSize : function() {
			var box = this.getDom('content');
			return uiUtils.getClientRect(box);
		},
		fitSize : function() {
			var popBodyEl = this.getDom('body');
			popBodyEl.style.width = '';
			popBodyEl.style.height = '';
			var size = this.mesureSize();
			popBodyEl.style.width = size.width + 'px';
			popBodyEl.style.height = size.height + 'px';
			return size;
		},
		showAnchor : function(element, hoz) {
			this.showAnchorRect(uiUtils.getClientRect(element), hoz);
		},
		showAnchorRect : function(rect, hoz, adj) {
			this._doAutoRender();
			var vpRect = uiUtils.getViewportRect();
			this._show();
			var popSize = this.fitSize();

			var sideLeft, sideUp, left, top;
			if (hoz) {
				sideLeft = this.canSideLeft
						&& (rect.right + popSize.width > vpRect.right && rect.left > popSize.width);
				sideUp = this.canSideUp
						&& (rect.top + popSize.height > vpRect.bottom && rect.bottom > popSize.height);
				left = (sideLeft ? rect.left - popSize.width : rect.right);
				top = (sideUp ? rect.bottom - popSize.height : rect.top);
			} else {
				sideLeft = this.canSideLeft
						&& (rect.right + popSize.width > vpRect.right && rect.left > popSize.width);
				sideUp = this.canSideUp
						&& (rect.top + popSize.height > vpRect.bottom && rect.bottom > popSize.height);
				left = (sideLeft ? rect.right - popSize.width : rect.left);
				top = (sideUp ? rect.top - popSize.height : rect.bottom);
			}

			var popEl = this.getDom();
			uiUtils.setViewportOffset(popEl, {
						left : left,
						top : top
					});
			domUtils.removeClasses(popEl, ANCHOR_CLASSES);
			popEl.className += ' '
					+ ANCHOR_CLASSES[(sideUp ? 1 : 0) * 2 + (sideLeft ? 1 : 0)];
		},
		showAt : function(offset) {
			var left = offset.left;
			var top = offset.top;
			var rect = {
				left : left,
				top : top,
				right : left,
				bottom : top,
				height : 0,
				width : 0
			};
			this.showAnchorRect(rect, false, true);
		},
		_show : function() {
			if (this._hidden) {
				var box = this.getDom();
				box.style.display = '';
				this._hidden = false;
				// if (box.setActive) {
				// box.setActive();
				// }
				this.fireEvent('show');
			}
		},
		isHidden : function() {
			return this._hidden;
		},
		show : function() {
			this._doAutoRender();
			this._show();
		},
		hide : function(notNofity) {
			if (!this._hidden && this.getDom()) {
				// this.getDom().style.visibility = 'hidden';
				this.getDom().style.display = 'none';
				this._hidden = true;
				if (!notNofity) {
					this.fireEvent('hide');
				}
			}
		},
		queryAutoHide : function(el) {
			return !el || !uiUtils.contains(this.getDom(), el);
		}
	};
	utils.inherits(Popup, UIBase);

	domUtils.on(document, 'mousedown', function(evt) {
				var el = evt.target || evt.srcElement;
				closeAllPopup(el);
			});
	domUtils.on(window, 'scroll', function() {
				closeAllPopup();
			});

	// var lastVpRect = uiUtils.getViewportRect();
	// domUtils.on( window, 'resize', function () {
	// var vpRect = uiUtils.getViewportRect();
	// if (vpRect.width != lastVpRect.width || vpRect.height !=
	// lastVpRect.height) {
	// closeAllPopup();
	// }
	// } );

})();
